// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package interceptor

import (
	"crypto/tls"
	"fmt"
	"net/http"

	"code.geniusai.org/notebook-manager/pkg/config"
	"code.geniusai.org/notebook-manager/pkg/resource"
)

const HEAD_SUFFIX = "@sangfor.com"

// ID
type ParamID struct {
	ID     string `uri:"id" binding:"required"`
	UserID string `uri:"userID" binding:"required"`
}

type HttpInterceptorHandler struct {
	resourceManager *resource.Manager // 资源的管理器
	confData        *config.Conf
	httpClient      *http.Client
}

func NewHttpInterceptorHandler(resourceManager *resource.Manager, confData *config.Conf) *HttpInterceptorHandler {
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}
	ret := &HttpInterceptorHandler{resourceManager: resourceManager, confData: confData, httpClient: client}
	return ret
}

func (h *HttpInterceptorHandler) GetUserNotebookId(userID string) ([]string, error) {
	ids, err := h.resourceManager.GetUserNotebookId(userID)
	return ids, err
}

func (h *HttpInterceptorHandler) doReqCookie(url string, cookie string) (*http.Response, error) {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("GET failed, err:%v\n\n", err)
		return nil, err
	}
	req.Header.Add("Cookie", cookie)
	resp, err := h.httpClient.Do(req)
	return resp, err
}
